﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class OptionUI : MonoBehaviour {
    public Toggle toggle;
    public string description
    {
        get
        {
            if (myOption != null)
            {
                return myOption.description;
            }
            else
            {
                return string.Empty;
            }
        }
    }
    [HideInInspector]
    public OptionContent myOption;
    public delegate void OnSelectedOptionContent(OptionContent myOption);
    public event OnSelectedOptionContent onSelected;
    public event OnSelectedOptionContent onDeselected;

    RectTransform rectTransform;
    InputField inputField;
    InputFieldScaler scaler;
    protected virtual void Awake()
    {
        rectTransform = GetComponent<RectTransform>();
        inputField = GetComponentInChildren<InputField>();
        scaler = GetComponentInChildren<InputFieldScaler>();
    }
    protected virtual void Start() 
    {
        scaler.onSizeChanged += SetSize;
    }

    public virtual void SetOptionUI(OptionContent option)
    {
        gameObject.SetActive(true);
        toggle.onValueChanged.AddListener(OnSelected);
        inputField.onValueChanged.AddListener(SetDescription);
        myOption = option;
        inputField.text = option.description;
    }

    public void Clear()
    {
        myOption = null;
        inputField.text = string.Empty;
        toggle.onValueChanged.RemoveAllListeners();
        inputField.onValueChanged.RemoveAllListeners();
        clear_event(onSelected);
        clear_event(onDeselected);
        toggle.isOn = false;
        gameObject.SetActive(false);
    }
    public void RemoveBinding()
    {
        myOption = null;
        inputField.text = string.Empty;
        toggle.isOn = false;
        toggle.onValueChanged.RemoveAllListeners();
        inputField.onValueChanged.RemoveAllListeners();
        clear_event(onSelected);
        clear_event(onDeselected);
    }
    /// <summary>
    /// 清除事件
    /// </summary>
    /// <param name="clearEvent"></param>
    void clear_event(OnSelectedOptionContent clearEvent)
    {
        Delegate[] dels = clearEvent.GetInvocationList();
        foreach (Delegate d in dels)
        {
            object delObj = d.GetType().GetProperty("Method").GetValue(d, null);
            string funcName = (string)delObj.GetType().GetProperty("Name").GetValue(delObj, null);
            clearEvent -= d as OnSelectedOptionContent;
        }
    }
    void SetDescription(string s)
    {
        if (myOption != null)
        {
            myOption.description = s;
        }
    }
    void OnSelected(bool b)
    {
        if (b)
        {
            if (onSelected != null)
            {
                onSelected(myOption);
            }
        }
        else
        {
            if (onDeselected != null)
            {
                onDeselected(myOption);
            }
        }
    }
    void SetSize(Vector2 newSize)
    {
        rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, newSize.y);

        RectTransform contentRT = transform.parent.GetComponent<RectTransform>();
        float h = LayoutUtility.GetFlexibleHeight(contentRT);
        if (h != contentRT.rect.height)
        {
            contentRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, h);
        }
        transform.parent.GetComponent<VerticalLayoutGroup>().SetLayoutVertical();
    }
    
}
